package com.frt.sygang.sevice.impl;/**
 * Created by 冯瑞涛 on 2022/9/18 16:57
 */

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.frt.sygang.common.CustomException;
import com.frt.sygang.sevice.DishService;
import com.frt.sygang.sevice.SetmealService;
import com.frt.sygang.entity.Category;
import com.frt.sygang.entity.Dish;
import com.frt.sygang.entity.Setmeal;
import com.frt.sygang.mapper.CategoryMapper;
import com.frt.sygang.sevice.CategoryService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * @author 冯瑞涛
 * @version 1.0
 * @date 2022/9/18 16:57
 */
@Service
public class CategoryServiceImpl extends ServiceImpl<CategoryMapper, Category> implements CategoryService {
    @Autowired
    private SetmealService setmealService;
    @Autowired
    private DishService dishService;

    @Override
    public void remove(Long ids) {
        LambdaQueryWrapper<Setmeal> setmealWrapper = new LambdaQueryWrapper<>();
        setmealWrapper.eq(Setmeal::getCategoryId, ids);
        int setmealCount = setmealService.count(setmealWrapper);
        if (setmealCount > 0) {
            throw new CustomException("该类型还存在关联套餐");
        }
        LambdaQueryWrapper<Dish> dishWrapper = new LambdaQueryWrapper<>();
        dishWrapper.eq(Dish::getCategoryId, ids);
        int dishCount = dishService.count(dishWrapper);
        if (dishCount > 0) {
            throw new CustomException("该类型还存在关联菜品");
        }
        super.removeById(ids);
    }
}
